Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a retain cycle in PostgreSQLConnection #116

Merged
merged 1 commit into from
Feb 19, 2019
Merged

Fix a retain cycle in PostgreSQLConnection #116

merged 1 commit into from
Feb 19, 2019

Conversation

baarde
Copy link
Contributor

@baarde baarde commented Nov 30, 2018

By adding a handler that retains self on the channel's close future, the connection gets retained until the channel is closed. We break the retain cycle by using a weak reference to self. The completion handler in send(:onResponse:) is enough to ensure that the connection stays alive while a message is sent to the server.

By adding a handler that retains self on the channel's close future, the connection gets retained until the channel is closed. We break the retain cycle by using a weak reference to self. The completion handler in `send(:onResponse:)` is enough to ensure that the connection stays alive while a message is sent to the server.
@baarde baarde changed the base branch from master to 1 February 15, 2019 16:53
@baarde
Copy link
Contributor Author

baarde commented Feb 15, 2019

Changing base branch to 1 as master is unaffected.

@tanner0101 tanner0101 added the bug Something isn't working label Feb 16, 2019
Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good change, but I also think that we should be verifying (at least in debug builds) that close() is called before a connection deinits.

Something like this:

final class PostgresConnection { 
    deinit {
        assert(self.isClosed, "PostgresConnection.close() not called before deinit")
    }
}

That way we can ensure code is properly cleaning up resources allocated by the connection.

@baarde
Copy link
Contributor Author

baarde commented Feb 18, 2019

Currently the connection automatically closes in deinit. Isn't it the intended behavior?

https://github.com/vapor/postgresql/blob/c9978744e6ddb2b0dc42858706c10de30c1f78ae/Sources/PostgreSQL/Connection/PostgreSQLConnection.swift#L134

@tanner0101
Copy link
Member

@baarde ah yeah, you are right. This LGTM. 👍

@tanner0101 tanner0101 merged commit 9f706b5 into vapor:1 Feb 19, 2019
@penny-coin
Copy link

Hey @baarde, you just merged a pull request, have a coin!

You now have 7 coins.

@baarde baarde deleted the fix-connection-closure-retain-cycle branch January 15, 2020 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants